#include "stm32f4xx_hal.h"
#include "main.h"

SRAM_HandleTypeDef hsram;

void HAL_FSMC_GPIO_MspInit(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  __HAL_RCC_FSMC_CLK_ENABLE();			                            //使能FSMC时钟
  __HAL_RCC_GPIOE_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();

  GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_14 | GPIO_PIN_15 |
                        GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_8 | GPIO_PIN_9 |
                        GPIO_PIN_10| GPIO_PIN_12 | GPIO_PIN_7;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF12_FSMC;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  
  GPIO_InitStruct.Pin = GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 |
                        GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14|
                        GPIO_PIN_15;
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
}

void HAL_FSMC_MspInit(void)
{
  /* USER CODE BEGIN FSMC_Init 0 */

  /* USER CODE END FSMC_Init 0 */

  FSMC_NORSRAM_TimingTypeDef Timing = {0};
  FSMC_NORSRAM_TimingTypeDef ExtTiming = {0};

  /* USER CODE BEGIN FSMC_Init 1 */

  /* USER CODE END FSMC_Init 1 */

  /** Perform the SRAM1 memory initialization sequence
  */
  hsram.Instance = FSMC_NORSRAM_DEVICE;
  hsram.Extended = FSMC_NORSRAM_EXTENDED_DEVICE;
  /* hsram.Init */
  hsram.Init.NSBank = FSMC_NORSRAM_BANK1;
  hsram.Init.DataAddressMux = FSMC_DATA_ADDRESS_MUX_DISABLE;
  hsram.Init.MemoryType = FSMC_MEMORY_TYPE_SRAM;
  hsram.Init.MemoryDataWidth = FSMC_NORSRAM_MEM_BUS_WIDTH_16;
  hsram.Init.BurstAccessMode = FSMC_BURST_ACCESS_MODE_DISABLE;
  hsram.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW;
  hsram.Init.WrapMode = FSMC_WRAP_MODE_DISABLE;
  hsram.Init.WaitSignalActive = FSMC_WAIT_TIMING_BEFORE_WS;
  hsram.Init.WriteOperation = FSMC_WRITE_OPERATION_ENABLE;
  hsram.Init.WaitSignal = FSMC_WAIT_SIGNAL_DISABLE;
  hsram.Init.ExtendedMode = FSMC_EXTENDED_MODE_ENABLE;
  hsram.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE;
  hsram.Init.WriteBurst = FSMC_WRITE_BURST_DISABLE;

	Timing.AddressSetupTime=0x02;       	
	Timing.AddressHoldTime=0;
	Timing.DataSetupTime=0x0C;			
	Timing.BusTurnAroundDuration=0x00;
	Timing.CLKDivision=0x00;
	Timing.DataLatency=0x00;
	Timing.AccessMode=FSMC_ACCESS_MODE_A;//模式A

	ExtTiming.AddressSetupTime=0x09;          	
	ExtTiming.AddressHoldTime=0x00;
	ExtTiming.DataSetupTime=0x09;        
	ExtTiming.BusTurnAroundDuration=0;	
	ExtTiming.CLKDivision=0;
	ExtTiming.DataLatency=0;
	ExtTiming.AccessMode=FSMC_ACCESS_MODE_A;    //模式A

  if (HAL_SRAM_Init(&hsram, &Timing, &ExtTiming) != HAL_OK)
  {
    //Error_Handler();
  }

  __FSMC_NORSRAM_ENABLE(FSMC_NORSRAM_DEVICE, FSMC_NORSRAM_BANK1);
}









